import axios from "axios";
import { ElMessage } from "element-plus";
function errorCodeTips(code: Number) {
  switch (code) {
    case 401:
      break;
  }
}
const service = axios.create({
  baseURL: process.env.VUE_BASE_URL,
  withCredentials: true,
  timeout: 10000,
});
service.defaults.headers.post["content-Type"] =
  "application/x-www-form-urlencoded";
// 请求头
service.interceptors.request.use(
  (config) => {
    // 请求头token验证
    const token = null;
    if (token) {
      config.headers["Authorization"] = token;
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

// 响应头
service.interceptors.request.use(
  (response) => {
    const res = response.data;
    if (res.code != 200) {
      // 对应的错误code提示
      errorCodeTips(res.code);
      return Promise.reject(new Error(res.msg || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    console.log(error);
    ElMessage({
      message: error.msg,
      type: "error",
      duration: 5 * 1000,
    });
    return Promise.reject(error);
  }
);

export default service;
